<?php
/**
 * Created by PhpStorm.
 * User: tom_xia
 * Date: 2018/8/13
 * Time: 18:50
 * 网春----小程序二维码
 */
require "login_filter.php";
use app\cls\tom\JsSDK;
use app\cls\tom\Response;
use app\cls\tom\SysResource;
use app\cls\tom\Tools;

$dataStr = file_get_contents("php://input");
if(empty($dataStr)){
    Response::badRequest();
}
$data = @json_decode($dataStr,true);
if(empty($data)){
    Response::badRequest();
}


if(empty($data["material_id"])){
    Response::badRequest();
}

$fileNameKey = md5($dataStr);
$urlKey = REDIS_PREFIX."wangchun_material_poster_{$fileNameKey}";
$redis = SysResource::getRedis();
$url = $redis->get($urlKey);
if($url){
    Response::output(0,"ok",["file_path"=>$url]);
}else{
    $d=[];
    //创建上传目录
    $date1=date("Y",time());
    $date2=date("m",time());
    $date3=date("d",time());
    $dir="/wangchun_poster_qrcode/".$date1."/".$date2."/".$date3;
    $savePath1 = CDN_PATH.$dir."/";
    $savePath2 = CDN_TMP2.$dir."/";
    $saveFile = $savePath1.$fileNameKey.".png";
    $saveFile2 = $savePath2.$fileNameKey.".png";
    $d['saveFile']=$saveFile;
    $d['saveFile2']=$saveFile2;


    if(!file_exists($savePath1)){
        $isCreateSuccess = mkdir($savePath1,0777,true);
        chmod($savePath1,0777);
        if(!$isCreateSuccess){
            Response::output(-1,"服务器内部异常-内部权限不足1");
        }
    }
    if(!file_exists($savePath2)){
        $isCreateSuccess2 = mkdir($savePath2,0777,true);
        chmod($savePath2,0777);
        if(!$isCreateSuccess2){
            Response::output(-1,"服务器内部异常-内部权限不足2");
        }
    }

    if(file_exists($saveFile) &&  filesize($saveFile) >0){
        $url = str_replace(CDN_PATH,CDN_HOST,$saveFile);
    }else{

        $materialId = $data["material_id"];
        unset($data["material_id"]);
        $newDataStr = json_encode($data);
        $url = "";

        $appKey = WX_PUBLIC_APP_ID;
        $appSecret = WX_PUBLIC_APP_SECRECT;

        $jssdk = new JsSDK($appKey, $appSecret);
        try{
            $accessToken = $jssdk->getAccessToken(1);
            $data  = http_build_query($data);
            $re = Tools::curl("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}",$newDataStr,"post",false,["Content-Type:application/json"]);
            $res = @json_decode($re,true);
            if ($res && isset($res["errcode"])) {
                $re123 = $res["errcode"] . "--" . $_SERVER['SERVER_ADDR'];
                if($res["errcode"]==40001){
                    $accessToken = $jssdk->getAccessTokenRefresh();
//                    $re = Tools::curl("https://api.weixin.qq.com/wxa/getwxacode?access_token={$accessToken}", $newDataStr, "post", false, ["Content-Type:application/json"]);
                    $re = Tools::curl("https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token={$accessToken}",$newDataStr,"post",false,["Content-Type:application/json"]);
                }else{
                    Response::output(-1, "请求失败 msg==>{$re}{$re123}:{$res["errmsg"]}");
                }
            }
            file_put_contents($saveFile2,$re);
            $cmd="mv {$saveFile2} {$saveFile}";
            exec($cmd);
            $d['cmd']=$cmd;
            if(file_exists($saveFile) && filesize($saveFile) >0){
                $url = str_replace(CDN_PATH,CDN_HOST,$saveFile);
            }else{
                Response::output(-1,"服务器内部异常-写存储异常");
            }
            $d['url']=$url;
//            var_dump($d);exit;
        }catch (\Exception $e){
//            Response::output(-1,"请求失败 msg==>".$e->getMessage());
            Response::output(-1,"请求失败 msg==>".$_SERVER['SERVER_ADDR'].$e->getMessage());
        }
    }
    $redis->setex($urlKey,10*60,$url);
    Response::output(0,"ok",["file_path"=>$url]);
}